import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GPAOperations extends JFrame implements ActionListener
{
	//Singleton
	private static GPAOperations go=null;
	
	JButton  btnSearch,btnExit;
	JLabel lblHeading,lblStudentNo,lblStudentName,lblBatch,lblStudentNameVal,lblBatchDesc,lblGPA,lblGPAVal,lblClass,lblClassVal;
	JTextField txtStudentNo;
	
	private GPAOperations()
	{
		//Outer Panel
		JPanel screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );		
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));
		//
		//Inner Panel
		JPanel screenSub=new JPanel( );
		screenSub.setLayout( new GridLayout( 6,0,8,8 ));		
		//
		
		//Labels
		//Heading
		lblHeading=new JLabel(ProgramUitl.CALCULATE_GPA_TEXT);
		Font fontHeading=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.HEADING_FONT_STLYE,
									ProgramUitl.HEADING_FONT_SIZE);
		lblHeading.setFont(fontHeading);
		screenMain.add("North",lblHeading);		
		//
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);
		
		lblStudentNo=new JLabel(ProgramUitl.STUDNET_NO_TEXT);						
		lblStudentNo.setFont(fontLbl);
		
		lblStudentName=new JLabel(ProgramUitl.STUDNET_NAME_TEXT);						
		lblStudentName.setFont(fontLbl);			
		
		lblStudentNameVal=new JLabel(ProgramUitl.EMPTY);						
		lblStudentNameVal.setFont(fontLbl);	

		lblBatch=new JLabel(ProgramUitl.BATCH_DESC_TEXT);						
		lblBatch.setFont(fontLbl);
		
		lblBatchDesc=new JLabel(ProgramUitl.EMPTY);						
		lblBatchDesc.setFont(fontLbl);		
		
		lblGPA=new JLabel(ProgramUitl.GPA_GPA_VALUE);						
		lblGPA.setFont(fontLbl);
		
		lblGPAVal=new JLabel(ProgramUitl.EMPTY);						
		lblGPAVal.setFont(fontLbl);
		
		lblClass=new JLabel(ProgramUitl.GPA_CLASS_VALUE);						
		lblClass.setFont(fontLbl);
		
		lblClassVal=new JLabel(ProgramUitl.EMPTY);						
		lblClassVal.setFont(fontLbl);		
		
		//

		//Text Boxes
		txtStudentNo=new JTextField();		
		//
		
		//Buttons
		btnSearch=new JButton(ProgramUitl.SEARCH_TEXT);
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		
		btnSearch.addActionListener(this);
		btnExit.addActionListener(this);
		//

		//Layout Design
		
		screenSub.add(lblStudentNo);
		screenSub.add(txtStudentNo);
		
		screenSub.add(lblStudentName);
		screenSub.add(lblStudentNameVal);

		
		screenSub.add(lblBatch);
		screenSub.add(lblBatchDesc);
		
		screenSub.add(lblGPA);
		screenSub.add(lblGPAVal);
		
		screenSub.add(lblClass);
		screenSub.add(lblClassVal);
		
		screenSub.add(btnSearch);		
		screenSub.add(btnExit);		
		//
		
		//Inner panel added to Outer Panel 
		screenMain.add("Center",screenSub);
		//
		
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});


		
		setSize(ProgramUitl.SMALL_SCREEN_WIDTH,ProgramUitl.SMALL_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(true);		
		setVisible(true);
		
		validate();
		//			
	}
	
	public static GPAOperations getInstance()
	{
		if(go==null)
		{	
			go=new GPAOperations();
		}
		
		return go;
	}
	//
	
	//Methods
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.CALCULATE_GPA_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				MainMenu.getInstance().setEnabled(true);
				MainMenu.getInstance().setVisible(true);
				go=null;
				this.dispose();
		} 
	}
	public void clearText()
	{		
		txtStudentNo.setText(null);
		lblBatchDesc.setText(null);
		lblStudentNameVal.setText(null);
		lblGPAVal.setText(null);
		lblClassVal.setText(null);
		txtStudentNo.requestFocus();		
	}	
	void seachStudent()
	{		
		if(isValidInputs())
		{	
			Student s=XmlOperations.getInstance().searchStudentByNo(txtStudentNo.getText().trim());	
			if(s!=null)
			{	
				lblStudentNameVal.setText(s.getName());
				Batch b=XmlOperations.getInstance().searchBatchByNo(Integer.toString(s.getBatchCode()));
				lblBatchDesc.setText(b.getBatchDesc());	
				float gpa=Calculations.getInstance().calculateGPA(XmlOperations.getInstance().getStudentEnrolInfo(Integer.toString(s.getNo())));
				lblGPAVal.setText(Float.toString(gpa));
				if(gpa>0.0F)
				{
					lblClassVal.setText(Calculations.getInstance().calculateClass(gpa));
				}
				else
				{
					lblClassVal.setText(ProgramUitl.GPA_CLASS_NA);
				}
				
				b=null;
			}
			else
			{				
				JOptionPane.showMessageDialog(null,ProgramUitl.OBJECT_NOT_FOUND,ProgramUitl.CALCULATE_GPA_TEXT,JOptionPane.ERROR_MESSAGE);
				clearText();
			}
		}
	}

	public boolean isValidInputs()
	{
		String msg="";

		if(ProgramUitl.IsEmptyString(txtStudentNo.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentNo.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.isPositiveWithNumeric(txtStudentNo.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentNo.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		
		if(ProgramUitl.IsEmptyString(msg)!=true)
		{
			JOptionPane.showMessageDialog(null,msg,ProgramUitl.CALCULATE_GPA_TEXT,JOptionPane.ERROR_MESSAGE);
		}
		
		return ProgramUitl.IsEmptyString(msg);		
			
	}
		
	//
	
	//Events
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}	
		else if(ae.getSource().equals(btnSearch))
		{			
			seachStudent();			
		}		
	}	
	//	
}